//
//  ViewController.m
//  百思不得姐
//
//  Created by LinBrong on 16/8/24.
//  Copyright © 2016年 LinBrong. All rights reserved.
//



#define channel @[@"全部",@"视频",@"图片",@"段子",@"声音"]

#import "BRCrossingViewController.h"
#import "BRCrossingModel.h"
#import "BRCrossingCollectionViewController.h"
#import <Foundation/Foundation.h>



@interface BRCrossingViewController ()<UICollectionViewDelegate>

/*    */
@property (strong, nonatomic) NSMutableArray  *dataArr;


/*  channels  */
@property (strong, nonatomic) NSMutableArray <UILabel *>*channelArr;



/*  collectionViewController  */
@property (strong, nonatomic) BRCrossingCollectionViewController *collectionVc;


/*  上一次选中的lab  */
@property (weak, nonatomic) UILabel *selectedLab;


/*    */
@property (weak, nonatomic) UIView *lineView;



@end

@implementation BRCrossingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.view.userInteractionEnabled = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    _dataArr = [NSMutableArray arrayWithCapacity:5];
    
    _channelArr = [NSMutableArray array];
    self.navigationItem.title = @"穿越";
    

    
    //创建频道lable
    [self creatChannels];
    
    
    
    //一进来相当于点击了第一个lab
    [self changeChannelAction:self.channelArr[0].gestureRecognizers.lastObject];
    
    BRCrossingCollectionViewController *collectionVc = [[BRCrossingCollectionViewController alloc]init];
    collectionVc.view.frame = CGRectMake(0, 108, self.view.bounds.size.width, self.view.bounds.size.height - 108);
    collectionVc.collectionView.delegate = self;
    [self.view addSubview:collectionVc.view];
    
    self.collectionVc = collectionVc;
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"cellFollowClickIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(loadNewData)];
    
}
- (void)loadNewData{
    
    UILabel *lab = self.selectedLab;
    
    [self changeChannelAction:lab.gestureRecognizers.lastObject];
    
}

- (void)creatChannels {
    //    static NSArray *const channel = @[@"全部",@"视频",@"图片",@"段子",@"声音"];
    
    UIView *channelsView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 44)];
    [self.view addSubview:channelsView];
    
    
    
    CGFloat labW = self.view.bounds.size.width / 5;
    CGFloat labH = 42;
    for (int i = 0; i < channel.count; i++) {
        
        UILabel *channelLab = [[UILabel alloc]initWithFrame:CGRectMake(i * labW, 0, labW, labH)];
        
        //        channelLab.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
        channelLab.textAlignment = NSTextAlignmentCenter;
        channelLab.font = [UIFont systemFontOfSize:17];
        channelLab.text = channel[i];
        channelLab.tag = i;
        channelLab.userInteractionEnabled = YES;
        
        [self.channelArr addObject:channelLab];
        //        [channelArr setObject:channelLab atIndexedSubscript:i];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeChannelAction:)];
        [channelLab addGestureRecognizer:tap];
        
        
        
        
        [channelsView addSubview:channelLab];
        
        if(i == 0){
            [self changeChannelAction:tap];
            //            self.selectedLab = channelLab;
            //            channelLab.textColor = [UIColor redColor];
        }
    }
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(16, labH, labW - 32, 2)];
    lineView.backgroundColor = [UIColor redColor];
    self.lineView = lineView;
    [channelsView addSubview:lineView];
}


- (void)changeChannelAction:(UITapGestureRecognizer *)tap {
    
    self.selectedLab.textColor = [UIColor blackColor];
    
    UILabel *lab = (UILabel *)tap.view;
    
    lab.textColor = [UIColor redColor];
    //刷新数据
    [self.collectionVc downloadDataWith:lab.tag];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lab.tag inSection:0];
    [self.collectionVc.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
    self.selectedLab = lab;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if([scrollView isKindOfClass:[UICollectionView class]]){
        
        NSInteger index =  self.collectionVc.collectionView.contentOffset.x /[UIScreen mainScreen].bounds.size.width ;
        
        UILabel *lab = self.channelArr[index];
        
        
        [self changeChannelAction:lab.gestureRecognizers.lastObject];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    self.lineView.transform = CGAffineTransformMakeTranslation(self.collectionVc.collectionView.contentOffset.x / 5, 0);
}
-(void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;

}

@end
